package org.veridiandynamics.client.view;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.domain.Appointment;
import org.veridiandynamics.server.domain.Doctor;
import org.veridiandynamics.server.domain.Patient;

import com.jgoodies.forms.factories.FormFactory;
import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

/**
 * @author Talia
 * 
 * The panel that displays after a patient logs in that displays all tasks a patient can complete.
 *
 */
public class PatientPanel extends AbstractPanelLayout
{
	private JComboBox timeBox;
	private String[] timeVals;
	private JComboBox moBox;
	private JComboBox dayBox;
	private JComboBox yearBox;
	private String[] minVals;
	private JTextField reasonText;
	private JButton schedule;
	private JButton histButton;
	private JPanel thisPanel;
	private String[] months;
	private String[] days;
	private String[] years;

	private Map<String, Object> appointmentMap;
	private int role;
	private String username;
	private JComboBox minBox;
	private Object logdummy;

	/**
	 * @param x The month selected to be displayed in panel.
	 * @return integer representing the month
	 */
	public static int convert(String x)
	{
		int retVal = 0;
		if(x.equals("Jan")) retVal = 0; 
		if(x.equals("Feb")) retVal = 1; 
		if(x.equals("Mar")) retVal = 2; 
		if(x.equals("Apr")) retVal = 3; 
		if(x.equals("May")) retVal = 4; 
		if(x.equals("Jun")) retVal = 5; 
		if(x.equals("Jul")) retVal = 6; 
		if(x.equals("Aug")) retVal = 7; 
		if(x.equals("Sep")) retVal = 8; 
		if(x.equals("Oct")) retVal = 9; 
		if(x.equals("Nov")) retVal = 10; 
		if(x.equals("Dec")) retVal = 11; 
		return retVal;
	}

	/**
	 * Constructor for panel. Creates panel.
	 * 
	 * @param username Username for patient
	 */
	public PatientPanel(String username, final Object logdummy)
	{
		this.logdummy = logdummy;
		this.username = username;
		numCenterPanels = 1;
		initialize();
		thisPanel = this;

		panelLeft.setLayout(new FormLayout(new ColumnSpec[] {
				FormFactory.RELATED_GAP_COLSPEC,
				FormFactory.DEFAULT_COLSPEC,
				FormFactory.RELATED_GAP_COLSPEC,
				ColumnSpec.decode("default:grow"),},
				new RowSpec[] {
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				FormFactory.DEFAULT_ROWSPEC,
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("max(65dlu;default):grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),
				FormFactory.RELATED_GAP_ROWSPEC,
				RowSpec.decode("default:grow"),}));

		JLabel lblCreateAn = new JLabel("1.  Create an Appointment");
		panelLeft.add(lblCreateAn, "4, 2");

		JPanel panel = new JPanel();
		panelLeft.add(panel, "4, 4, left, fill");

		JLabel lblPleaseEnterThe = new JLabel("Please enter the time:");
		panel.add(lblPleaseEnterThe);

		timeVals = new String[10];
		for (int i = 0; i < timeVals.length; i++)
		{
			if (i < 5)
				timeVals[i] = (i + 8) + "";
			else
				timeVals[i] = (i - 4) + "";
		}
		timeBox = new JComboBox(timeVals);
		panel.add(timeBox);

		minVals = new String[4];
		minVals[0] = "00";
		minVals[1] = "15";
		minVals[2] = "30";
		minVals[3] = "45";
		minBox = new JComboBox(minVals);
		panel.add(minBox);

		JPanel panel_1 = new JPanel();
		panelLeft.add(panel_1, "4, 6, left, fill");

		JLabel lblPleaseEnterThe_1 = new JLabel("Please enter the date:");
		panel_1.add(lblPleaseEnterThe_1);

		months = new String[12];
		months[0] = "Jan";
		months[1] = "Feb";
		months[2] = "Mar";
		months[3] = "Apr";
		months[4] = "May";
		months[5] = "Jun";
		months[6] = "Jul";
		months[7] = "Aug";
		months[8] = "Sep";
		months[9] = "Oct";
		months[10] = "Nov";
		months[11] = "Dec";
		moBox = new JComboBox(months);
		panel_1.add(moBox);

		days = new String[30];
		for (int i = 0; i < days.length; i++)
		{
			days[i] = (i + 1) + "";
		}
		dayBox = new JComboBox(days);
		panel_1.add(dayBox);

		years = new String[2];
		Calendar calendar = Calendar.getInstance();
		years[0] = "2010";
		years[1] = "2011";
		yearBox = new JComboBox(years);
		panel_1.add(yearBox);

		JPanel panel_2 = new JPanel();
		panelLeft.add(panel_2, "4, 8, left, fill");

		JLabel lblPleaseEnterA = new JLabel("Please enter a reason for your appointment: ");
		panel_2.add(lblPleaseEnterA);

		reasonText = new JTextField();
		panel_2.add(reasonText);
		reasonText.setColumns(10);
		ButtonGroup group = new ButtonGroup();

		JPanel panel_4 = new JPanel();
		panelLeft.add(panel_4, "4, 12, left, fill");

		schedule = new JButton("Schedule");
		ButtonListener listener = new ButtonListener();
		schedule.addActionListener(listener);
		panel_4.add(schedule);

		JPanel panel_5 = new JPanel();
		panelLeft.add(panel_5, "4, 14, left, fill");

		JLabel lblViewMedical = new JLabel("2.  View Medical History   ");
		panel_5.add(lblViewMedical);

		histButton = new JButton("View History");
		histButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent event)
			{
				MedicalHistoryPanel histCard = new MedicalHistoryPanel(c1, thisPanel);
				thisPanel.add(histCard, "HISTCARD");
				c1.show(thisPanel, "HISTCARD");	
			}
		});
		panel_5.add(histButton);
	}

	/**
	 * All of the data is collected here and placed in a hashmap to be sent to the 
	 * server for processing.
	 * 
	 * @author Lee
	 */
	private class ButtonListener implements ActionListener
	{
		private String time;
		private String mDate;
		private String dDate;
		private String yDate;
		private String mins;
		private int month;

		public void actionPerformed(ActionEvent event)
		{		
			if (event.getSource() == schedule)
			{
				if (reasonText.getText().equals(""))
				{
					JOptionPane.showMessageDialog(null, "Please fill out all forms");
				}

				else
				{
					appointmentMap = new HashMap<String, Object>();
					time = timeVals[timeBox.getSelectedIndex()];
					mDate = months[moBox.getSelectedIndex()];
					month = convert(mDate);
					dDate = days[dayBox.getSelectedIndex()];
					yDate = years[yearBox.getSelectedIndex()];
					String reason = reasonText.getText();
					mins = minVals[minBox.getSelectedIndex()];
					Date date = new Date(Integer.parseInt(yDate) - 1900, month,
							Integer.parseInt(dDate), Integer.parseInt(time), 
							Integer.parseInt(mins));
					Date endDate = calcEndDate();
					Appointment appointment = new Appointment(date, endDate, reason, null);
					Patient patient = new Patient();
					patient = (Patient) logdummy;
					Doctor doctor = patient.getMyDoctor();
					if (!doctor.getSchedule().checkConflict(appointment))
					{
						doctor.getSchedule().addAppointment(appointment);
						patient.setMyAppointments(new HashSet<Appointment>());
						patient.getMyAppointments().add(appointment);
						HibernateDriver driver = new HibernateDriver();
						driver.UpdateObject(patient);
						appointmentMap.put("requestType", "makeAppointment");
						appointmentMap.put("appointment", appointment);
						appointmentMap.put("username", username);

						JOptionPane.showMessageDialog(null, "Appointment created " +
								"at " + time + ":" + mins + " on " + mDate + " " + dDate + " " + 
								yDate);
					}
					else JOptionPane.showMessageDialog(null, "Woops, there is a conflict!");
				}
			}
		}

		/**
		 * @param date Date object for start time.
		 * 
		 * @return returns end date
		 */
		private Date calcEndDate()
		{
			Date date = new Date(Integer.parseInt(yDate)- 1900, month,
					Integer.parseInt(dDate), Integer.parseInt(time) + 1, 
					Integer.parseInt(mins));
			return date;
		}
	}
}